package com.fho4565.gui;

import com.fho4565.commands.DisplayCommand;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.datafixers.util.Pair;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.Gui;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.world.scores.Score;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.server.ServerLifecycleHooks;

import java.util.List;

@OnlyIn(Dist.CLIENT)
public class CustomGUI extends Gui {
    private final PoseStack poseStack;
    private final Font font = this.getFont();

    public CustomGUI(PoseStack poseStack) {
        super(Minecraft.getInstance());
        this.poseStack = poseStack;
    }
    public void drawJSON(Component json, int x, int y) {
        drawString(poseStack, getFont(), json, x, y, 0xFFFFFF);
    }
    public void render() {
        DisplayCommand.displayList.renderAll(this);
    }

    public void drawScore(String objectiveName, int x, int y) {
        Objective objective = ServerLifecycleHooks.getCurrentServer().getScoreboard().getObjectives().stream().filter(obj -> obj.getName().equals(objectiveName)).findFirst().orElseThrow();
        displayScoreboard(poseStack, objective, x, y);
    }

    private void displayScoreboard(PoseStack poseStack, Objective objective, int x, int y) {
        List<Pair<Score, Component>> formattedScores = objective.getScoreboard().getPlayerScores(objective)
                .stream()
                .filter(score -> !score.getOwner().startsWith("#"))
                .sorted((score1, score2) -> (score1.getScore() > score2.getScore()) ? -1 : ((score1 == score2) ? 0 : 1))
                .limit(15)
                .map(score -> {
                    PlayerTeam team = objective.getScoreboard().getPlayersTeam(score.getOwner());
                    Component formattedName = PlayerTeam.formatNameForTeam(team, new TextComponent(score.getOwner()));
                    return Pair.of(score, formattedName);
                })
                .toList();
        int scoreCount = formattedScores.size();
        int backgroundColorLight = this.minecraft.options.getBackgroundColor(0.4F);
        int backgroundColorDark = this.minecraft.options.getBackgroundColor(0.3F);

        int maxWidth;
        int maxNameWidth = 0 ,maxScoreWidth = 0;

        for (Pair<Score, Component> pair : formattedScores) {
            Score score = pair.getFirst();
            String name = pair.getSecond().getString();
            maxNameWidth = Math.max(maxNameWidth, name.length()*6);
            maxScoreWidth = Math.max(maxScoreWidth, (String.valueOf(ChatFormatting.RED) + score.getScore()).length() * 6);
        }
        maxWidth = maxNameWidth;
        for (int i = 0; i < scoreCount; i++) {
            Pair<Score, Component> pair = formattedScores.get(i);
            Score score = pair.getFirst();
            Component playerDisplayName = pair.getSecond();
            String scoreString = String.valueOf(ChatFormatting.RED) + score.getScore();

            int lineY = y + i * 9;
            int lineWidth = maxScoreWidth + (10-scoreString.length())*6;

            font.draw(poseStack, playerDisplayName, x + 2, (float) lineY + 9, -1);
            font.draw(poseStack, scoreString,  x + 2 + maxWidth + lineWidth - 48, (float) lineY + 9, -1);

            if (i == 0) {
                customFill(poseStack, x, y, x + 2 + maxWidth + maxScoreWidth, 9, backgroundColorLight);
                customFill(poseStack, x, y + 9, x + 2 + maxWidth + maxScoreWidth, scoreCount * 9, backgroundColorDark);
                font.draw(poseStack, objective.getDisplayName(), x+ (float) (x + 2 + maxWidth + maxScoreWidth - objective.getName().length() * 6) /2, (float) y, -1);
            }

        }
    }

    private void customFill(PoseStack poseStack, int x, int y, int width, int height, int color) {
        fill(poseStack, x, y, x + width, y + height, color);
    }
}

